package com.hjq.mallproduct.service.impl;

import com.hjq.common.constants.ProductConstants;
import com.hjq.mallproduct.dao.AttrAttrgroupRelationDao;
import com.hjq.mallproduct.entity.AttrAttrgroupRelationEntity;
import com.hjq.mallproduct.entity.AttrEntity;
import com.hjq.mallproduct.entity.vo.*;
import com.hjq.mallproduct.service.AttrAttrgroupRelationService;
import com.hjq.mallproduct.service.AttrService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallproduct.dao.AttrGroupDao;
import com.hjq.mallproduct.entity.AttrGroupEntity;
import com.hjq.mallproduct.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
@Transactional
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> groupEntityQueryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (catId == 0) {
            groupEntityQueryWrapper.eq("attr_group_id", key).or().like("attr_group_name", key).or().like("descript", key);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    groupEntityQueryWrapper
            );

            return new PageUtils(page);
        } else {
            //select * from pms_attr_group where catelog_id=1 and (attr_group_id=key or attr_group_name like %key% or descript kie %key% )
            groupEntityQueryWrapper.eq("catelog_id", catId);
            if (!StringUtils.isEmptyOrWhitespaceOnly(key)) {
                groupEntityQueryWrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key).or().like("descript", key);

                });

            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    groupEntityQueryWrapper);

            return new PageUtils(page);

        }

    }



    @Override
    public List<AttrEntity> queryAllAttrPage(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<AttrEntity> attrEntities = attrgroupRelationEntities.stream().map((item) -> {
            Long attrId = item.getAttrId();
            return attrService.getById(attrId);

        }).collect(Collectors.toList());
        return attrEntities;
    }

    @Override
    public void deleteAttrRelation(List<AttrRelationVO> attrRelationVOList) {

        relationDao.deleteBatchByAttrRelation(attrRelationVOList) ;




    }

    @Override
    public PageUtils queryPageOfNotInAttrGroup(Map<String, Object> params, Long attrGroupId) {
        //找出没有分组的在同一catId下的AttrGroup
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroupId);
        Long catelogId = attrGroupEntity.getCatelogId();


        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
        wrapper.eq("catelog_id", catelogId).eq("attr_type", ProductConstants.AttrEnum.ATTR_BASE.code);
        //查看是否有关键字
        if (!StringUtils.isEmptyOrWhitespaceOnly(key)) {
            wrapper.eq("attr_id",key).or().like("attr_name",key).or().like("value_select",key);
        }


        List<AttrEntity> attrEntities = attrService.getBaseMapper().selectList(wrapper);


        List<AttrEntity> attrEntityList = attrEntities.stream().filter((attrEntity -> {
            //返回没有在AttrGroup下的Attr
            AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            return relationEntity == null;
        })).collect(Collectors.toList());
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()

        );
        PageUtils utils = new PageUtils(page);
        utils.setList(attrEntityList);

        return utils;
    }

    @Override
    public List<AttrGroupVO>   getAllGroupWithAttr(Long catId) {
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        List<AttrGroupVO> attrGroupVOList = attrGroupEntities.stream().map((attrGroupEntity -> {
            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupVO);
            //查询下面的attr
            Long attrGroupId = attrGroupEntity.getAttrGroupId();

            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            //group底下没有东西
            if(relationEntities.size()==0){
                 attrGroupVO.setAttrs(new ArrayList<AttrVO>());
                 return  attrGroupVO;
            }
            List<Long> attrIdList = relationEntities.stream()
                    .map(AttrAttrgroupRelationEntity::getAttrId)
                    .collect(Collectors.toList());

            List<AttrEntity> attrEntities = attrService.getBaseMapper().selectBatchIds(attrIdList);
            List<AttrVO> attrVOList = attrEntities.stream().map(attrEntity -> {
                AttrVO attrVO = new AttrVO();
                BeanUtils.copyProperties(attrEntity, attrVO);
                return attrVO;
            }).collect(Collectors.toList());
            attrGroupVO.setAttrs(attrVOList);
            return attrGroupVO;
        })).collect(Collectors.toList());

        return  attrGroupVOList;
    }

    @Override
    public List<SpuItemAttrGroupVO> getAttrGroupWithAttrBySpuId(Long spuId, Long catId) {
        //查出当前spu的分组和值
        List<SpuItemAttrGroupVO> spuItemAttrGroupVOS= baseMapper.getSpuItemAttrGroupVO(spuId,catId);
        return spuItemAttrGroupVOS;
    }


}

